/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.yournamehere.client;
import com.google.gwt.user.client.ui.*;
/**
 *
 * @author D4v1D
 */
import com.google.gwt.user.client.ui.Widget;
import java.util.Iterator;
/**
 *
 * @author D4v1D
 */
public class SudokuPanel extends Grid{
    private boolean complete = false;

    public class MyTableListener implements TableListener {
        public void onCellClicked(SourcesTableEvents sender, int row, int col) {
            new SudokuInputDialog(SudokuPanel.this,row,col).show();
        }
    }
    public SudokuPanel(){
        super(9,9);
        this.setBorderWidth(1);
        this.setSize("400","400");
        this.addTableListener(new MyTableListener());
        for(int i=0;i<9;++i){
            for(int j=0;j<9;j+=3){
                this.getCellFormatter().addStyleName(i, j,"delimiter-vertical");
                this.getCellFormatter().addStyleName(j, i, "delimiter-horizontal");
            }
            this.getCellFormatter().addStyleName(8, i, "delimiter-last-horizontal");
            this.getCellFormatter().addStyleName(i, 8, "delimiter-last-vertical");
        }
    }
    public boolean checkValidity(String value,int row,int col){
        try{
           int number=Integer.parseInt(value);
           if(number>10 || number<1){

               new SudokuDialog("You have to give an integer between 1 to 9").show();
                return false;

           }
           else{
                if(checkVerticalSum(col)){
                    if(checkHorizontalSum(row)){
                       if(!checkSquareSum(row,col)){
                          new SudokuDialog("Incorrect value, value already attributed in the square").show();
                          return false;
                       }
                   }else{
                       new SudokuDialog("Incorrect value, value already attributed in the row").show();
                       return false;
                   }
                }else{
                    new SudokuDialog("Incorrect value, value already attributed in the column").show();
                    return false;
                }
                
           }
        }catch(Exception e){
            new SudokuDialog(e.toString()).show();
            return false;
        }
        boolean localComplete = true;
        for(int i=0;i<9;++i){
            for(int j=0;j<9;j++){
                String s = this.getText(i, j);
                Label l = new Label();
                l.setText(s);
                this.add(l);
                if (s.equals("") || s == null) {
                    localComplete = false;
                    break;
                }
            }
        }
        complete = localComplete;
        return true;
    }
    public boolean checkVerticalSum(int col){
        boolean[] tab = new boolean[9];
        for(int i=0;i<9;i++){
            String text = this.getText(i, col);
            try{
                int val = Integer.parseInt(text);
                if(tab[val]){
                    return false;
                }else{
                    tab[val]=true;
                }
            }catch(NumberFormatException e){
                //do nothing, no value has been set in the cell
            }catch(Exception e){
                new SudokuDialog(e.toString()).show();
                return false;
            }
        }
        return true;
    }
    public boolean checkHorizontalSum(int row){
        boolean[] tab = new boolean[9];
        for(int i=0;i<9;i++){
            String text = this.getText(row, i);
            try{
                int val = Integer.parseInt(text);
                if(tab[val]){
                    return false;
                }else{
                    tab[val]=true;
                }
            }catch(NumberFormatException e){
                //do nothing, no value has been set in the cell
            }catch(Exception e){
                new SudokuDialog(e.toString()).show();
                return false;
            }
        }
        return true;
    }
    public boolean checkSquareSum(int row,int col){
        boolean[] tab = new boolean[9];
        int x = row/3*3;
        int y = col/3*3;
        for(int i=0;i<3;++i){
            for(int j=0;j<3;++j){
                String text = this.getText(x+i, y+j);
                try{
                    int val = Integer.parseInt(text);
                    if(tab[val]){
                        return false;
                    }else{
                        tab[val]=true;
                    }
                }catch(NumberFormatException e){
                    //do nothing, no value has been set in the cell
                }catch(Exception e){
                    new SudokuDialog(e.toString()).show();
                    return false;
                }
            }
        }
        return true;
    }
    public boolean loadGrid(int[][] values){
        for(int i=0;i<9;i++){
            for(int j=0;j<9;j++){
                if(values[i][j] != 0){
                    this.setText(i, j, Integer.toString(values[i][j]));
                }
            }
        }
        return true;
    }

    public boolean isComplete() {
        return this.complete;
    }
}
